package zhieasy.cn.service.book_market.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.book_market.UserBook;
import zhieasy.cn.persistence.mapper.book.UserBookMapper;
import zhieasy.cn.service.book_market.SortService;

import java.util.List;

/**
 * @author wangzeya
 * @create 2020-08-13 11:14:00
 * 排序
 */

@Service

public class SortServiceImpl implements SortService {
    @Autowired
    private UserBookMapper userBookMapper;

    /** 按价格排序
     *
     * @param bookId
     * @param bookOrder
     * @return
     */
    @Override
    public ResultVO<List<UserBook>> priceSort(String bookId, String bookOrder , int pageIndex , int pageSize) {
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id", bookId);
        if ("up".equals(bookOrder)) {
            wrapper.orderByAsc("sell_price");
        }else if ("down".equals(bookOrder)) {
            wrapper.orderByDesc("sell_price");
        }else {
            return ResultVOUtil.error(702,"bookOrder传递参数有误");
        }
        List<UserBook> bookList = userBookMapper.selectList(wrapper);
        return ResultVOUtil.success(bookList);

    }

    /** 按热度排序
     *
     * @param bookId
     * @param bookOrder
     * @return
     */
    @Override
    public ResultVO<List<UserBook>> clicksSort(String bookId, String bookOrder, int pageIndex , int pageSize) {
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id", bookId);
        if ("up".equals(bookOrder)) {
            wrapper.orderByAsc("clicks");
        }else if ("down".equals(bookOrder)) {
            wrapper.orderByDesc("clicks");
        }else {
            return ResultVOUtil.error(702,"bookOrder传递参数有误");
        }
        List<UserBook> bookList = userBookMapper.selectList(wrapper);
        return ResultVOUtil.success(bookList);

    }

    /** 按新旧程度排序
     *
     * @param bookId
     * @param bookOrder
     * @return
     */
    @Override
    public ResultVO<List<UserBook>> degreeSort(String bookId, String bookOrder, int pageIndex , int pageSize) {
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id", bookId);
        if ("up".equals(bookOrder)) {
            wrapper.orderByAsc("degree");
        }else if ("down".equals(bookOrder)){
            wrapper.orderByDesc("degree");
        }else {
            return ResultVOUtil.error(702,"bookOrder传递参数有误");
        }
        List<UserBook> bookList = userBookMapper.selectList(wrapper);
        return ResultVOUtil.success(bookList);

    }

}
